package com.dpxm.zdbdg.chart.daletou.service;

import com.dpxm.zdbdg.chart.daletou.dao.DaLeTouDao;
import com.dpxm.zdbdg.chart.daletou.vo.JXZuHeVO;
import com.dpxm.zdbdg.chart.daletou.vo.QueryDaLeTouVO;
import com.dpxm.zdbdg.chart.daletou.vo.coldHotRes;
import com.dpxm.zdbdg.chart.daletou.vo.coldHotResYiBai;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vehiclebase.enums.QueryEnum;
import vehiclebase.utils.ToolsUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HundredService {
    @Autowired
    private DaLeTouDao daLeTouDao;

    /**
     * 1——100期热号分析冷热号
     * @return
     */
    public coldHotResYiBai coldHot() {
        coldHotResYiBai coldHot = new coldHotResYiBai();
        //查询出最近100期开奖信息
        List<QueryDaLeTouVO> queryMessage = daLeTouDao.QueryMessage(QueryEnum.QUERY_QIHAO.val());
        List<Integer> hong = new ArrayList<>();
        //此集合为100期内的所有开奖号码。 16期内出险过4次的号码为热号
        if(ToolsUtils.notEmpty(queryMessage)){
            queryMessage.forEach(a->{
                hong.add(a.getQ_one());
                hong.add(a.getQ_two());
                hong.add(a.getQ_three());
                hong.add(a.getQ_four());
                hong.add(a.getQ_five());
            });
        }
        //热冷号分析 （）
        getHotNumb(hong,coldHot);
        //组装返回的数据
        getMessage(coldHot);

        return coldHot;
    }

    public void  getHotNumb(List<Integer> hong,coldHotResYiBai coldHot){
        Map<Integer,Integer> map = new HashMap<>();
        for(Integer str:hong){
            Integer i = 1; //定义一个计数器，用来记录重复数据的个数
            if(map.get(str) != null){
                i=map.get(str)+1;
            }
            map.put(str,i);
        }
        //通过ArrayList构造函数把map.entrySet()转换成list
        List<Map.Entry<Integer, Integer>> mappingList = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
        //通过比较器进行比较排序
        Collections.sort(mappingList, new Comparator<Map.Entry<Integer, Integer>>(){
            public int compare(Map.Entry<Integer, Integer> mapping1, Map.Entry<Integer, Integer> mapping2){
                return mapping1.getValue().compareTo(mapping2.getValue());
            }
        });
        //热号的集合
        List<Integer> hotNumb = new ArrayList<>();
        //100最热的七个好
        for (int i=mappingList.size()-7 ;i<mappingList.size() ; i++){
            hotNumb.add(mappingList.get(i).getKey());
           // System.out.println(mappingList.get(i).getKey()+":"+mappingList.get(i).getValue());
        }
        //最冷号的集合
        List<Integer> cotNumb = new ArrayList<>();
        cotNumb.add(mappingList.get(0).getKey());

        //排序
        Collections.sort(hotNumb);
        //List<Integer> 转化为List<String>
        List<String> hotNumbs = hotNumb.stream().map(String::valueOf).collect(Collectors.toList());
        List<String> hotNumbsss = cotNumb.stream().map(String::valueOf).collect(Collectors.toList());
        coldHot.setHotNumberYiBai(hotNumbs);
        coldHot.setColdNumberYiBai(hotNumbsss);
        /*for(Map.Entry<Integer,Integer> mapping:mappingList){
            System.out.println(mapping.getKey()+":"+mapping.getValue());
        }*/



    }

    /**
     * 组装返回的数据
     * @param coldHot
     * @return
     */
    public void  getMessage(coldHotResYiBai coldHot){
        //冷号
        StringBuffer coldNumber = new StringBuffer();
        coldNumber.append("冷号：");
        List<String> coldList = coldHot.getColdNumberYiBai();
        for (int i=0 ;i<coldList.size() ; i++){
            coldNumber.append(" "+coldList.get(i));
        }

        //热号
        StringBuffer hotNumber = new StringBuffer();
        hotNumber.append("热号：");
        List<String> hotList = coldHot.getHotNumberYiBai();
        for (int i=0 ;i<hotList.size() ; i++){
            hotNumber.append(" "+hotList.get(i));
        }

        //调整红球的格式
        StringBuffer Number = new StringBuffer();
        List<JXZuHeVO> numberList = new ArrayList<>();//返回机选五注的List

        //机选五注
        for (int k=0 ;k<5 ; k++){
            JXZuHeVO jXZuHeVO = new JXZuHeVO();
            //机选 冷号
            Random random = new Random();
            int num = random.nextInt(coldList.size());

            System.out.println(coldList.get(num));
            //机选 热号
            List<String> jxHotNumber = new ArrayList<>();
            for (int i=0 ;i<20 ; i++){
                Random randomss = new Random();
                int nums = randomss.nextInt(hotList.size());
                String shu=hotList.get(nums);
                //判断jxHotNumber是否含有shu
                if (jxHotNumber.stream().filter(w->String.valueOf(w.toString()).equals(shu)).findAny().isPresent()){
                    continue;
                }
                jxHotNumber.add(shu);
                System.out.println(jxHotNumber.toString());
                if(jxHotNumber.size()==4){
                    break;
                }
            }
            //把冷号也放进去
            jxHotNumber.add(coldList.get(num));
            //机选两位篮球
            List<String> modes = new ArrayList<>();
            for (int i=1; i<=12; i++){ //模拟1-12
                modes.add(String.valueOf(i));
            }
            //最终篮球的号码
            List<String> jxLanHotNumber = new ArrayList<>();
            for (int i=0 ;i<20 ; i++){
                Random randomss = new Random();
                int nums = randomss.nextInt(modes.size());
                String shu=modes.get(nums);
                if (jxLanHotNumber.stream().filter(w->String.valueOf(w.toString()).equals(shu)).findAny().isPresent()){
                    continue;
                }
                jxLanHotNumber.add(shu);
                if(jxLanHotNumber.size()==2){
                    break;
                }
            }
            //List<String> 转化为List<Integer>
            List<Integer> codesInteger = jxHotNumber.stream().map(Integer::parseInt).collect(Collectors.toList());
            //排序
            Collections.sort(codesInteger);
            //最终的红球
            List<String> cotNumbs = codesInteger.stream().map(String::valueOf).collect(Collectors.toList());
            //List<String> 转化为List<Integer>
            List<Integer> codesIntegerlanqiu = jxLanHotNumber.stream().map(Integer::parseInt).collect(Collectors.toList());
            //排序
            Collections.sort(codesIntegerlanqiu);
            //最终的篮球
            List<String> lanqiu = codesIntegerlanqiu.stream().map(String::valueOf).collect(Collectors.toList());
            Number.append("第"+k+"注: ");
            StringBuffer hong = new StringBuffer();
            StringBuffer lan = new StringBuffer();
            for (int i=0 ;i<cotNumbs.size() ; i++){
                String str="";
                if(cotNumbs.get(i).length()==1){
                    str="0"+cotNumbs.get(i);
                }else {
                    str=cotNumbs.get(i);
                }
                Number.append(str+" ");
                hong.append(str+" ");
            }
            Number.append(" + ");
            for (int j=0 ;j<lanqiu.size() ; j++){
                String str="";
                if(lanqiu.get(j).length()==1){
                    str="0"+lanqiu.get(j);
                }else {
                    str=lanqiu.get(j);
                }
                Number.append(str+" ");
                lan.append(str+" ");
            }
            jXZuHeVO.setBenqiHongQiuNumber(hong.toString());
            jXZuHeVO.setBenqiLanQiuNumber(lan.toString());
            numberList.add(jXZuHeVO);
            Number.append(" | ");



        }

        coldHot.setNumbersYiBai(Number.toString());
        coldHot.setBenqiColdNumberYiBai(coldNumber.toString());
        coldHot.setBenqiHotNumberYiBai(hotNumber.toString());
        coldHot.setNumberListYiBai(numberList);
    }

}
